<?php

include_once MODELS.'Annalisses/Annalisses.php';

class Database{

    private static $annalisses = null;

    public static function connect() {
        if(is_null(self::$annalisses)){
            $dbconfig = simplexml_load_file(MODELS."dbconfig.xml");
            $prepare = new Prepare($dbconfig->host, $dbconfig->user, $dbconfig->password);
            $prepare->setDBName($dbconfig->dbname);
                    $dbms = (int) $dbconfig->dbms;
            $annalisses = new Annalisses(Annalisses::DBMS($dbms), $prepare);        
            $con = $annalisses->connect();
            
            if($con) self::$annalisses = $annalisses;
            else { self::$annalisses = null; return false; }
        }
        return true;
    }

    protected function getAnnalisses(){
        return self::$annalisses;
    }

    protected function insert($table, $fields, $values){
        $q = "INSERT INTO ".$table." ";
        if(!empty($fields))
        	$q .= "(".$fields.") ";
        if(is_array($values)){
            $q .= "VALUES ".implode(",",$values);
        } else {
            $q .= "VALUES (".$values.")";
        }
        //echo $q."<br/>";
        $q = self::$annalisses->execute($q);
        return (bool)$q;
    }

    protected function select($table, $fields = " * ", $condition = null, $limit = null){
        $q = "SELECT ";
        $q .= $fields." ";
        $q .= "FROM ".$table." ";
        if(!empty($condition))
        	$q .= " WHERE ".$condition;
        if(!empty($limit))
            $q .= " LIMIT ".$limit;
        //echo $q."<br/>";
        $q = self::$annalisses->execute($q);
        return $q;
    }

    protected function update($table, $field, $value, $condition){
        $q = "UPDATE ".$table;
        $q .= " SET ".$field." = ".$value;
        $q .= " WHERE ".$condition;
        //echo $q."<br/>";
        $q = self::$annalisses->execute($q);
        return $q;
    }

    protected function updates($table, $fields, $values, $condition){
        $q = "UPDATE ".$table;
        $q .= " SET ";
        $c = count($fields);
        for($i = 0; $i < $c; $i++){
                $q .= $fields[$i]." = ".$values[$i].", ";
        }
        $q = substr($q, 0, strlen($q)-2);
        $q .= " WHERE ".$condition;
        //echo $q;
        $q = self::$annalisses->execute($q);
        return $q;
    }
	
    protected function formattedUpdates($table, $values, $condition){
        $q = "UPDATE ".$table;
        $q .= " SET ".$values;
        $q .= " WHERE ".$condition;
        //echo $q;
        $q = self::$annalisses->execute($q);
        return $q;
    }

    protected function delete($table, $condition){
        $q = "DELETE FROM ".$table;
        $q .= " WHERE ".$condition;
        //echo $q;
		$q = self::$annalisses->execute($q);
		return $q;
    }

    //BEGIN Transactions session

    public function begin(){
        return self::$annalisses->beginTransc();
    }	

    public function rollBack(){
        return self::$annalisses->rollBackTransc();
    }

    public function commit() {
        return self::$annalisses->commitTransc();
    }

    //END Transactions session


    public function isConnected(){
        $con = self::$annalisses->getConnection(self::NAME);
        return (bool) $con;
    }

    public function errorCode(){
        return self::$annalisses->errorCode();
    }

    public static function close(){
        self::$annalisses->closeConnection();
        self::$annalisses = null;
    }

    public static function turnInValues($dic){
        $attrs = array();
        if(is_object($dic)){
            $vars = get_object_vars($dic);
            foreach ($vars as $val){
                $values[] = $val; 
            }
            $dic = $values;
        }
        foreach($dic as $attr){
            if(is_null($attr) || empty($attr)) $attrs[] = "NULL";
            else if(is_numeric($attr) && !is_string($attr)) $attrs[] = $attr;
            else if(is_bool($attr)) $attrs[] = $attr ? "TRUE" : "FALSE";
            else $attrs[] = "\"$attr\"";
        }
        $res = implode(",", $attrs);
        return $res;
    }

    public static function turnInUpdateValues($dic){
        $attrs = array();
        foreach($dic as $attr => $value){
            $_attr = $attr." = ";
            if(is_null($value) || empty($value)) $_attr .= "NULL";
            else if(is_numeric($value) && !is_string($value)) $_attr .= $value;
            else if(is_bool($attr)) $_attr .= $attr ? "TRUE" : "FALSE";
            else  $_attr .= "\"$value\"";
            $attrs[] = $_attr;
        }
        $res = implode(",", $attrs);
        return $res;
    }

    public function handleError(){
        $errorCode = $this->errorCode();
        return "Mensagem de erro padrão: $errorCode";
    }
    
}
?>
